package com.example.demo.utils;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @ClassName AsyncTest
 * @Author l42142
 * @Date 2021/3/5 14:51
 * @Description TODO
 * @Version 1.0
 **/
@Component
public class AsyncTest {
    @Async
    public void test1(AsyncTest asyncTest) {
        long start = System.currentTimeMillis();
        System.out.println("test1 start = " + start);
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            sum = sum + i;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("test1 end = " + end);
        System.out.println("test1 = " + (end - start));
    }

    @Async
    public void test2(AsyncTest asyncTest) {
        long start = System.currentTimeMillis();
        System.out.println("test2 start = " + start);
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            sum = sum + i;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("test2 end = " + end);
        System.out.println("test2 = " + (end - start));
    }

    public void test() {
        long start = System.currentTimeMillis();
        System.out.println("test start = " + start);
//        test1();
//        test2();
        long end = System.currentTimeMillis();
        System.out.println("test end = " + end);
        System.out.println("test = " + (end - start));
    }
}
